package io.qifan.desktop.server.controller.http;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import io.qifan.desktop.api.auth.Token;
import io.qifan.desktop.api.auth.request.UserLoginRequest;
import io.qifan.desktop.common.model.R;
import io.qifan.desktop.server.application.AuthApplicationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RequestMapping("auth")
@RestController
public class AuthController {
    private final AuthApplicationService applicationService;

    @PostMapping("login")
    public R<Token> login(@RequestBody UserLoginRequest loginRequest) {
        SaTokenInfo saTokenInfo = applicationService.login(loginRequest);

        return R.ok(new Token()
                .setTokenName(saTokenInfo.tokenName)
                .setUserId(StpUtil.getLoginIdAsLong())
                .setTokenValue(saTokenInfo.getTokenValue()));
    }
}
